package com.telecom.cms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dto.*;


public class SEG_ATOM_GROUP_RELDAO {
	private static List instlist=null;
	private static final String SQL_FINDBYID = "select * from " +
			Constant.SEG_ATOM_GROUP_REL +
			" where ATOM_GROUP_ID = ? order by 1";
	
	private static final String SQL_FINDBYATOMID = "select * from "+Constant.SEG_ATOM_GROUP_REL+" where BIZ_ATOM_ID = ? order by 1";
	private static final String SQL_FINDALL = "select * from "+Constant.SEG_ATOM_GROUP_REL + " order by 1 , 2"  ;
	
	private Connection conn = null;

	public SEG_ATOM_GROUP_RELDAO(Connection con) {

		this.conn = con;

	}

	public List findById(int id) {
		List list = new ArrayList();
		if(null==instlist)
			 findAll();
		for (int i=0;i<instlist.size();i++)
			if(((SEG_ATOM_GROUP_RELDTO)instlist.get(i)).getATOM_GROUP_ID()==id)
				list.add(instlist.get(i));
/*		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(SQL_FINDBYID);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				SEG_ATOM_GROUP_RELDTO dto = new SEG_ATOM_GROUP_RELDTO();
				dto.setBIZ_ATOM_ID(rs.getInt(1));
				dto.setATOM_GROUP_ID(rs.getInt(2));
				list.add(dto);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				
				if(rs !=null){
					
					rs.close();
				}
				if (ps != null)
				{	ps.close();}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		return list;
	}
	
	public List findByAtomId(int id) {
		List list = new ArrayList();
		if(null==instlist)
			 findAll();
		for (int i=0;i<instlist.size();i++)
			if(((SEG_ATOM_GROUP_RELDTO)instlist.get(i)).getBIZ_ATOM_ID()==id)
				list.add(instlist.get(i));
		/*
		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(SQL_FINDBYATOMID);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				SEG_ATOM_GROUP_RELDTO dto = new SEG_ATOM_GROUP_RELDTO();
				dto.setBIZ_ATOM_ID(rs.getInt(1));
				dto.setATOM_GROUP_ID(rs.getInt(2));
				list.add(dto);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				
				if(rs !=null){
					
					rs.close();
				}
				if (ps != null)
				{	ps.close();}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		return list;
	}

public List findAll() {
	if(instlist!=null)
		return instlist;
	PreparedStatement ps = null;
	ResultSet rs = null;
	List list = new ArrayList();
	try {
		ps = conn.prepareStatement(SQL_FINDALL);
		rs = ps.executeQuery();
		while (rs.next()) {
			SEG_ATOM_GROUP_RELDTO dto = new SEG_ATOM_GROUP_RELDTO();
			dto.setBIZ_ATOM_ID(rs.getInt(1));
			dto.setATOM_GROUP_ID(rs.getInt(2));
			list.add(dto);
		}

	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		try {
			
			if(rs !=null){
				
				rs.close();
			}
			if (ps != null)
			{	ps.close();}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	instlist=list;
	return list;
}
}